<?

function smarty_function_datefields_getRange( $min, $max ) {
	$values = array();
	for ( $i=$min; $i<=$max; $i++ ) {
		$value = new stdclass();
		$value->id = $i;
		$value->name = $i;
		$values[] = $value;
	}
	return $values;
}

function smarty_function_datefields( $params, $smarty ) {

	$smarty->depend( 'function', 'select' );

	$name = $params['name'];
	$value = $params['value'];
	$class = $params['class'];

	if ( $label = $params['label'] ) {
		$smarty->depend( 'function', 'label' );
		smarty_function_label(array(
			'for' => $name,
			text => $label
		), $smarty );
	}

	// work out date values if we have
	// something to work with.
	$year = null;
	$month = null;
	$day = null;
	if ( $value ) {
		$ts = date( 'U', strtotime($value) );
		$year = date( 'Y', $ts );
		$month = date( 'm', $ts );
		$day = date( 'd', $ts );
	}

	// year
	smarty_function_select(array(
		name => $name . '_year',
		from => smarty_function_datefields_getRange(2000,2010),
		selected => $year,
		'class' => $class
	), $smarty );

	// month
	smarty_function_select(array(
		name => $name . '_month',
		from => smarty_function_datefields_getRange(1,12),
		selected => $month,
		'class' => $class
	), $smarty );

	// day
	smarty_function_select(array(
		name => $name . '_day',
		from => smarty_function_datefields_getRange(1,31),
		selected => $day,
		'class' => $class
	), $smarty );

}

?>